package com.kly.chatter.api.application.controller.chatter;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.fqgj.base.services.redis.RedisClient;
import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.log.factory.LogFactory;
import com.fqgj.log.interfaces.Log;
import com.kly.chatter.api.application.controller.chatter.req.ChatterConversationLogReq;
import com.kly.chatter.api.application.controller.chatter.req.NewMatchListReq;
import com.kly.chatter.api.application.controller.user.req.MessageReq;
import com.kly.chatter.api.application.service.ChatterConversationLogService;
import com.kly.chatter.api.application.utils.Result;
import com.kly.sc.api.application.constant.ApiRedisKeys;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.sc.api.integration.userplatform.UserApiClient;
import com.kly.user.chatterService.ModelService;
import com.kly.user.dto.chatter.request.QueryNewModelReq;
import com.kly.user.dto.chatter.response.NewModel;
import com.kly.user.dto.chatter.response.NewModelsDto;
import com.kly.user.dto.chatter.response.UserAccount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author linqu
 */
@Api(value = "New Match", tags = {"New Match"})
@RestController
@RequestMapping("/api/user")
public class ModelController {

    private static final Log logger = LogFactory.getLog(ModelController.class);
    @Resource
    ModelService modelService;
    @Resource
    RedisClient redisClient;

    @Autowired
    private ChatterConversationLogService chatterConversationLogService;
    /**
     * 新关系列表接口
     */
    @ApiOperation("新关系列表接口")
    @RequestMapping(value = "/match/newMatchList", method = {RequestMethod.POST})
    @SentinelResource
    public Response newMatchList(@Valid @RequestBody NewMatchListReq newMatchListReq, BindingResult results) {
        if (results.hasErrors()) {
            return Response.error();
        }
        final QueryNewModelReq req = QueryNewModelReq.builder().chatterUserCode(newMatchListReq.getChatterUserCode()).build();
        final NewModelsDto result = RpcUtils.getResult(modelService.queryNewModelList(req));
        if (Objects.nonNull(result)) {
            List<NewModel> matchedList = result.getMatchedList();
            if (CollUtil.isNotEmpty(matchedList)){
                for (NewModel model:matchedList){
                    UserAccount userAccount = model.getUserAccount();
                    String userMember = redisClient.get(ApiRedisKeys.USER_MEMBER.getValue(), userAccount.getUserCode());
                    if (StringUtils.isNotEmpty(userMember)){
                        userAccount.setMember(true);
                    }
                }
            }
            return Response.ok(result);
        }
        return Response.ok();

    }


    @PostMapping("/addErrorLog")
    @ApiOperation(value = "model会话列表加载失败错误信息记录", notes = "model会话列表加载失败错误信息记录")
    @SentinelResource
    public Result sendFirstMessage(@RequestBody ChatterConversationLogReq chatterConversationLogReq) {
        chatterConversationLogService.addChatterConversationLog(chatterConversationLogReq);
        return Result.ok();
    }


    private static UserApiClient getUserApiClient() {
        return new UserApiClient("key:username", "secret:pwd");
    }

    public static void main(String[] args) {
        UserApiClient userApiClient = getUserApiClient();
        JSONObject postJson = new JSONObject();
        postJson.put("chatterUserCode", "1670054838275219460");

        final JSONObject newModelResponse = userApiClient.ChatterAPI.newMatchList(postJson);

        System.out.println(newModelResponse);


    }



}
